﻿#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import urllib
import urlparse

INDEX_NAME = "recipes_index"

def download_photo(img_url, filename):
    downloaded_image = file(filename, "wb")

    image_on_web = urllib.urlopen(img_url)
    while True:
        buf = image_on_web.read(65536)
        if len(buf) == 0:
            break
        downloaded_image.write(buf)
    downloaded_image.close()
    image_on_web.close()

def download_all_photos():
  IMAGES_DIR = os.path.join(os.getcwd(), "data/gotovim_doma_images")
  IMAGES_FILE = os.path.join(os.getcwd(), "data/gotovim_doma_images.txt")

  index = 0
  for line in open(IMAGES_FILE):
    line = line.strip()

    file = line
    if file.startswith('/img/'):
      file = file[5:]
    if file.startswith('img/'):
      file = file[4:]

    file = os.path.join(IMAGES_DIR, file)
    if os.path.exists(file):
      continue

    url = urlparse.urljoin("http://gotovim-doma.ru", line)
    download_photo(url, file)

download_all_photos()